﻿@{ var list = ViewBag.Names as List<HZYProject.Domain.Colleges>; }
@model HZYProject.Domain.Exam
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <input type="hidden" id="ExamID" name="ExamID" value="@Model.ExamID" />
                <label class="layui-form-label">真实姓名</label>
                <div class="layui-input-inline">
                    <input type="text" id="RealName" name="RealName" placeholder="" class="layui-input">
                </div>
                <label class="layui-form-label">所属院校</label>
                <div class="layui-input-inline">
                    <select id="CollegesID" name="CollegesID" lay-search="">
                        <option value="">--全部--</option>
                        @{
                            foreach (var item in list)
                            {
                                <option value="@item.CollegesID">@item.Name</option>
                            }
                        }
                    </select>
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        添加考试人员
    </button>
</script>

<script type="text/html" id="user-createTime">
    {{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
</script>

<script type="text/html" id="user-status">
    {{#if (d.status == 1) { }}
    <span>正常</span>
    {{# }else if(d.status == 2){ }}
    <span>锁定</span>
    {{# }else if(d.status == 3){ }}
    <span>冻结</span>
    {{# } }}
</script>

@section script{
    <script>
        layui.use(['table', 'form', 'jquery'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;

            let MODULE_PATH = "/Exam";

            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '序号',
                    align: 'left',
                    type: "numbers"
                },
                {
                    title: '学生编号',
                    field: 'studentID',
                    align: 'left'
                },
                {
                    title: '微信OpenID',
                    field: 'wXOpenID',
                    align: 'left'
                },
                {
                    title: '院校编号',
                    field: 'collegeId',
                    align: 'left'
                },
                {
                    title: '所属院校',
                    field: 'collegeName',
                    align: 'left'
                    //,templet: '#user-colleges'
                },
                {
                    title: '真实姓名',
                    field: 'realName',
                    align: 'left'
                },
                {
                    title: '身份证号',
                    field: 'idNo',
                    align: 'left'
                },
                {
                    title: '性别',
                    field: 'sex',
                    align: 'left'
                },
                {
                    title: '电话',
                    field: 'mphone',
                    align: 'left'
                },
                {
                    title: '邮箱',
                    field: 'email',
                    align: 'left'
                },
                {
                    title: '状态',
                    field: 'status',
                    align: 'left',
                    templet: '#user-status'
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'left',
                    templet: '#user-createTime'
                }]
            ]

            table.render({
                elem: '#user-table',
                url: '/Exam/setStu',
                method: 'post',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#user-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
                , done: function () {
                    $("[data-field='studentID']").css('display', 'none');
                    $("[data-field='wXOpenID']").css('display', 'none');
                    $("[data-field='collegeId']").css('display', 'none');
                }
            });

            table.on('toolbar(user-table)', function (obj) {
                if (obj.event === 'add') {
                    window.add(obj);
                } else if (obj.event === 'refresh') {
                    window.refresh();
                }
            });

            form.on('submit(user-query)', function (data) {
                table.reload('user-table', {
                    where: {
                        RealName: $("#RealName").val(),
                        CollegesID: $("#CollegesID").val()
                    }
                })
                return false;
            });
            var eid = $("#ExamID").val();
            window.add = function (obj) {
                let data = table.checkStatus(obj.config.id).data;
                if (data.length === 0) {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                } 
                let ids = "";
                for (let i = 0; i < data.length; i++) {
                    ids += data[i].studentID + ",";
                }
                ids = ids.substr(0, ids.length - 1);
                layer.confirm('确定要添加这些学生吗', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: MODULE_PATH + "/addStu",
                        method: 'post',
                        dataType: 'json',
                        data: {
                            'ids': ids,
                            'eid': eid
                        },
                        success: function (result) {
                            layer.close(loading);
                            if (result.success) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    window.parent.location.reload();
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }

            window.refresh = function (param) {
                table.reload('user-table');
            }

        })
    </script>
}
